package com.example.plugin.setting;

import com.example.plugin.config.DefaultTemplate;
import com.example.plugin.config.PanelModel;
import com.example.plugin.util.TemplateUtil;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.components.PersistentStateComponent;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.components.State;
import com.intellij.openapi.components.Storage;
import com.intellij.util.xmlb.XmlSerializerUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Settings
 *
 * @author lipan
 * @date 2022/1/16 21:01
 * @email 13160080997@163.com
 */
@State(name = "Generate", storages = @Storage("Generate.xml"))
public class Settings implements PersistentStateComponent<Settings> {

    /**
     * 模板名集合
     */
    public static String[] templateNames = {"entity.ftl","dao.ftl","service.ftl","serviceImpl.ftl","controller.ftl", "sqlXml.ftl"};

    /**
     * 模板Map
     */
    private Map<String, DefaultTemplate> templateGroup = new LinkedHashMap<>();

    /**
     * history Map
     */
    private Map<String, PanelModel> panelModelMap = new LinkedHashMap<>();

    @Nullable
    @Override
    public Settings getState() {
        return this;
    }

    @Override
    public void loadState(@NotNull Settings settings) {
        XmlSerializerUtil.copyBean(settings, this);
    }

    public void putPanelModel(String tableName, PanelModel panelModel){
        panelModelMap.put(tableName,panelModel);
    }

    public void put(DefaultTemplate defaultTemplate){
        this.templateGroup.put(defaultTemplate.getName(),defaultTemplate);
    }

    public PanelModel getPanelModel(String tableName){
        return panelModelMap.get(tableName);
    }

    public DefaultTemplate get(String name){
        return this.templateGroup.get(name);
    }

    public Settings() {
        init();
    }

    public void init(){
        templateGroup.putAll(TemplateUtil.loadTemplate(templateNames));
    }

    public static Settings getInstance() {
        return ApplicationManager.getApplication().getService(Settings.class);
    }

    public Map<String, DefaultTemplate> getTemplateGroup() {
        return templateGroup;
    }

    public void setTemplateGroup(Map<String, DefaultTemplate> templateGroup) {
        this.templateGroup = templateGroup;
    }

    public Map<String, PanelModel> getPanelModelMap() {
        return panelModelMap;
    }

    public void setPanelModelMap(Map<String, PanelModel> panelModelMap) {
        this.panelModelMap = panelModelMap;
    }
}
